# Concave
# Copyright (C) 2012 Daniel Malmqvist
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.


from pygame import Rect,Surface,init,image
from pygame.locals import SRCALPHA,RLEACCEL
import g
import os
def loadImage(sheet, indexX, indexY, gridSizeX=32, gridSizeY = 32):
    """
    Loads an image from a given sheet
    """
    rect = Rect((indexX*gridSizeX,indexY*gridSizeY, gridSizeX, gridSizeY))
            
    image = Surface(rect.size, SRCALPHA)
    
    image.blit(sheet, (0, 0), rect)
    image.set_colorkey(-1, RLEACCEL)
    return image




# Found at http://www.pygame.org/docs/ref/font.html under render comments:

# Simple functions to easily render pre-wrapped text onto a single
# surface with a uniform background.
# Author: George Paci

# Results with various background color alphas:
#  no background given: ultimate blit will just change figure part of text,
#      not ground i.e. it will be interpreted as a transparent background
#  background alpha = 0: "
#  background alpha = 128, entire bounding rectangle will be equally blended
#      with background color, both within and outside text
#  background alpha = 255: entire bounding rectangle will be background color
#
# (At this point, we're not trying to respect foreground color alpha;
# we could by compositing the lines first onto a transparent surface, then
# blitting to the returned surface.)
def renderLines(lines, font, antialias, color, background=None):
    fontHeight = font.get_height()

    surfaces = [font.render(ln, antialias, color) for ln in lines]
    # can't pass background to font.render, because it doesn't respect the alpha

    maxwidth = max([s.get_width() for s in surfaces])
    result = Surface((maxwidth, len(lines)*fontHeight), SRCALPHA)
    if background == None:
        result.fill((90,90,90,0))
    else:
        result.fill(background)

    for i in range(len(lines)):
        result.blit(surfaces[i], (0,i*fontHeight))
    return result

def renderTextBlock(text, font, antialias, color, background=None):
    "This is renderTextBlock"
    brokenText = text.replace("\r\n","\n").replace("\r","\n")
    return renderLines(brokenText.split("\n"), font, antialias, color, background)



def addBorder(image):
    """
    Adds border images to a given image
    """
    width = image.get_width()/16 + 2
    height = image.get_height()/16 + 2

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    
    borderImage.blit(image, (4,4))
    
    return borderImage


def addBorderCentered(image, width = -1):
    """
    Adds border images to a given image, the original image will be in the middle.
    """
    if width == -1:
        width = image.get_width()/16 + 2
    height = image.get_height()/16 + 2

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    x = (borderImage.get_width() - image.get_width()) / 2
    y = (borderImage.get_height() - image.get_height()) / 2
    
    borderImage.blit(image, (x,y))
    
    return borderImage


def addBorderFixedSize(image, x, y):
    """
    Adds border images with a fixed size, used in the menu 
    """
    width = x
    height = y

    rect = Rect(0,0,width*16, height*16)
    borderImage = Surface(rect.size, SRCALPHA)

    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                borderImage.blit(itemBox[0][0], (16*x, 16*y))
            elif x == 0 and y == height -1:
                borderImage.blit(itemBox[0][2], (16*x, 16*y))
            elif x == 0:
                borderImage.blit(itemBox[0][1], (16*x, 16*y))
            elif y == 0 and x == width - 1:
                borderImage.blit(itemBox[2][0], (16*x, 16*y))
            elif y == 0:
                borderImage.blit(itemBox[1][0], (16*x, 16*y))
            elif x == width - 1 and y == height -1:
                borderImage.blit(itemBox[2][2], (16*x, 16*y))
            elif x == width - 1:
                borderImage.blit(itemBox[2][1], (16*x, 16*y))
            elif y == height -1:
                borderImage.blit(itemBox[1][2], (16*x, 16*y))
            else:
                borderImage.blit(itemBox[1][1], (16*x, 16*y))
    
    x = (borderImage.get_width() - image.get_width()) / 2
    y = (borderImage.get_height() - image.get_height()) / 2
    
    borderImage.blit(image, (x,y))
    
    return borderImage
    
    
def drawImageTextBox(item, surface, yStart = 288, comparedTo = None):
    """
    Draws the information image for a given item.
    """
    itemText = renderTextBlock(item.getText(comparedTo), g.fontNormal, 1, (255,255,255))
        
    width = itemText.get_width()/16 + 2
    height = itemText.get_height()/16 + 2
    
    xStart = 448 - width*16
    
    for x in range (0, width):
        for y in range (0, height):
            if x == 0 and y == 0:
                surface.blit(itemBox[0][0], (xStart+16*x, yStart+16*y))
            elif x == 0 and y == height -1:
                surface.blit(itemBox[0][2], (xStart+16*x, yStart+16*y))
            elif x == 0:
                surface.blit(itemBox[0][1], (xStart+16*x, yStart+16*y))
            elif y == 0 and x == width - 1:
                surface.blit(itemBox[2][0], (xStart+16*x, yStart+16*y))
            elif y == 0:
                surface.blit(itemBox[1][0], (xStart+16*x, yStart+16*y))
            elif x == width - 1 and y == height -1:
                surface.blit(itemBox[2][2], (xStart+16*x, yStart+16*y))
            elif x == width - 1:
                surface.blit(itemBox[2][1], (xStart+16*x, yStart+16*y))
            elif y == height -1:
                surface.blit(itemBox[1][2], (xStart+16*x, yStart+16*y))
            else:
                surface.blit(itemBox[1][1], (xStart+16*x, yStart+16*y))
    surface.blit(itemText, (xStart+4,yStart+4))
    
init()

itemBoxA = image.load(os.path.join("graphic","itembox.png"))
itemBox = [ [ 0 for i in range(3) ] for j in range(3) ]
for x in range (0, 3):
    for y in range (0, 3):
        itemBox[x][y] = loadImage(itemBoxA,x,y, 16,16)
        